package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemRequestRemote;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.remote.ProductApiClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 清空购物车
 */

@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {

    @Autowired
    ProductApiClient productApiClient;

    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        ClearCartItemRequest request = new ClearCartItemRequest();
        request.setProductIds(createOrderContext.getBuyProductIds());
        request.setUserId(createOrderContext.getUserId());

        ClearCartItemRequestRemote requestRemote = new ClearCartItemRequestRemote();
        requestRemote.setUserId(request.getUserId());
        requestRemote.setUsername(createOrderContext.getBuyerNickName());
        requestRemote.setProductIds(request.getProductIds());

        ResponseData responseData = productApiClient.clearCartItemByUserID(requestRemote);

        System.out.println("responseData = " + responseData);

        return true;
    }
}
